#include "DebugNew.h"

void leakTest1()
{
  int* p = new int;
  // delete p; // memory leak
}

void leakTest2()
{
  char* p = new char[100];
  // delete p; // memory leak
}

void leakTest3()
{
  void* p = malloc(200);
  // free(p); // memory leak
}

void delTest1()
{
  int* p = new int;
  delete p;

  delete p;  // can not del
  delete(p); // can not del
}

void delTest2()
{
  int* p = new int;
  delete p;

  int* p2 = p;
  delete p2;  // can not del
  delete(p2); // can not del
}

void violationTest()
{
  int* p = (int*)0x1234;
  delete p;
  delete(p);
}

#include "MemoryLeak.h"

void basic_test()
{
  printf("\nleakTest1\n");
  MemoryLeak::start();
  leakTest1();
  MemoryLeak::stop();

  printf("\nleakTest2\n");
  MemoryLeak::start();
  leakTest2();
  MemoryLeak::stop();

  printf("\nleakTest3\n");
  MemoryLeak::start();
  leakTest3();
  MemoryLeak::stop();

  try
  {
    printf("\ndelTest1\n");
    MemoryLeak::start();
    delTest1();
    MemoryLeak::stop();
  } catch(...)
  {
  }

  printf("\ndelTest2\n");
  MemoryLeak::start();
  delTest2();
  MemoryLeak::stop();

  printf("\nviolationTest\n");
  MemoryLeak::start();
  violationTest();
  MemoryLeak::stop();

  printf("\ntest all\n");
  MemoryLeak::start();
  leakTest1();
  leakTest2();
  leakTest3();
  delTest1();
  delTest2();
  violationTest();
  MemoryLeak::stop();
}
